//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CompoundOption.h"
using namespace Cephei::QL::Experimental::Compoundoption;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (Cephei::QL::Instruments::IStrikedTypePayoff^ motherPayoff, Cephei::QL::IExercise^ motherExercise, Cephei::QL::Instruments::IStrikedTypePayoff^ daughterPayoff, Cephei::QL::IExercise^ daughterExercise, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CCompoundOption::typeid)
{
    CStrikedTypePayoff^ _CmotherPayoff;
    CExercise^ _CmotherExercise;
    CStrikedTypePayoff^ _CdaughterPayoff;
    CExercise^ _CdaughterExercise;
    try
    {
#ifdef HANDLE
        _phCompoundOption = NULL;
#endif
        _CmotherPayoff = safe_cast<CStrikedTypePayoff^> (motherPayoff);
        _CmotherPayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _motherPayoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_CmotherPayoff->GetShared ()); 
        _CmotherExercise = safe_cast<CExercise^> (motherExercise);
        _CmotherExercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _motherExercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_CmotherExercise->GetShared ()); 
        _CdaughterPayoff = safe_cast<CStrikedTypePayoff^> (daughterPayoff);
        _CdaughterPayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _daughterPayoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_CdaughterPayoff->GetShared ()); 
        _CdaughterExercise = safe_cast<CExercise^> (daughterExercise);
        _CdaughterExercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _daughterExercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_CdaughterExercise->GetShared ()); 
        _ppCompoundOption = new boost::shared_ptr<QuantLib::CompoundOption> (new QuantLib::CompoundOption ( _motherPayoff,  _motherExercise,  _daughterPayoff,  _daughterExercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCompoundOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CmotherPayoff != nullptr) _CmotherPayoff->Unlock();
        if (_CmotherExercise != nullptr) _CmotherExercise->Unlock();
        if (_CdaughterPayoff != nullptr) _CdaughterPayoff->Unlock();
        if (_CdaughterExercise != nullptr) _CdaughterExercise->Unlock();
    }
}
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (boost::shared_ptr<QuantLib::CompoundOption>& childNative, Object^ owner) : COneAssetOption(CCompoundOption::typeid)
{
#ifdef HANDLE
	_phCompoundOption = NULL;
#endif
	_ppCompoundOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
}
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (QuantLib::CompoundOption& childNative, Object^ owner) : COneAssetOption(CCompoundOption::typeid)
{
#ifdef HANDLE
	_phCompoundOption = NULL;
#endif
	_ppCompoundOption = new boost::shared_ptr<QuantLib::CompoundOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
    _CompoundOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (CCompoundOption^ copy) : COneAssetOption(CCompoundOption::typeid)
{
#ifdef HANDLE
	_phCompoundOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCompoundOption = new boost::shared_ptr<QuantLib::CompoundOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
    }
}
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (PLATFORM::Type^ t) : COneAssetOption(CCompoundOption::typeid)
{
#ifdef HANDLE
	_phCompoundOption = NULL;
#endif
	if (!t->IsSubclassOf(CCompoundOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (QuantLib::Handle<QuantLib::CompoundOption>& childNative, Object^ owner)  : COneAssetOption(CCompoundOption::typeid)
{
	_phCompoundOption = &childNative;
	_ppCompoundOption = &static_cast<boost::shared_ptr<QuantLib::CompoundOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
    _CompoundOptionOwner = owner;
}
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (QuantLib::Handle<QuantLib::CompoundOption> childNative)  : COneAssetOption(CCompoundOption::typeid)
{
	_phCompoundOption = &childNative;
	_ppCompoundOption = &static_cast<boost::shared_ptr<QuantLib::CompoundOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Compoundoption::CCompoundOption::CCompoundOption (QuantLib::CompoundOption childNative)  : COneAssetOption(CCompoundOption::typeid)
{
#ifdef HANDLE
	_phCompoundOption = NULL;
#endif
	_ppCompoundOption = new boost::shared_ptr<QuantLib::CompoundOption> (new QuantLib::CompoundOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCompoundOption));
}
#endif

Cephei::QL::Experimental::Compoundoption::CCompoundOption::~CCompoundOption ()
{
    if (_ppCompoundOption != NULL)
    {
	    delete _ppCompoundOption;
        _ppCompoundOption = NULL;
    }
}
Cephei::QL::Experimental::Compoundoption::CCompoundOption::!CCompoundOption ()
{
    if (_ppCompoundOption != NULL)
    {
	    delete _ppCompoundOption;
    }
}
QuantLib::CompoundOption& Cephei::QL::Experimental::Compoundoption::CCompoundOption::GetReference ()
{
    if (_ppCompoundOption == NULL) throw REFNEW NativeNullException ();
	return **_ppCompoundOption;
}
boost::shared_ptr<QuantLib::CompoundOption>& Cephei::QL::Experimental::Compoundoption::CCompoundOption::GetShared ()
{
    if (_ppCompoundOption == NULL) throw REFNEW NativeNullException ();
	return *_ppCompoundOption;
}
QuantLib::CompoundOption* Cephei::QL::Experimental::Compoundoption::CCompoundOption::GetPointer ()
{
    if (_ppCompoundOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppCompoundOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CompoundOption>& Cephei::QL::Experimental::Compoundoption::CCompoundOption::GetHandle ()
{
	if (_phCompoundOption == NULL)
	{
		_phCompoundOption = new Handle<QuantLib::CompoundOption> (*_ppCompoundOption);
	}
	return *_phCompoundOption;
}
#endif
bool Cephei::QL::Experimental::Compoundoption::CCompoundOption::HasNative () 
{
	return (_ppCompoundOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Compoundoption::ICompoundOption^ Cephei::QL::Experimental::Compoundoption::CCompoundOption_Factory::Create (Cephei::QL::Instruments::IStrikedTypePayoff^ motherPayoff, Cephei::QL::IExercise^ motherExercise, Cephei::QL::Instruments::IStrikedTypePayoff^ daughterPayoff, Cephei::QL::IExercise^ daughterExercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CCompoundOption ( motherPayoff,  motherExercise,  daughterPayoff,  daughterExercise,  QL_Pricer);
}
